#ifndef SPOINTERPOLICIES_HPP
#define SPOINTERPOLICIES_HPP

/********************************************************************
	created:	2011/01/14
	filename: 	E:\C++\Red\src\Utils\SPointerPolicies.hpp
	author:		Red
	
	purpose:	Different policies for a Smart Pointer (SPointer.hpp)
				First is simple Reference Counting policy
				Second is Resource referencing (Needs a Resource class which implements AddRef() & Release()
*********************************************************************/

#include <cstdlib>

namespace red {
	/// Reference counting policy
	template<class T>
	class RefCountPolicy{
	public:
		RefCountPolicy() : mCounter(new int(1)) {}

		/// Add a reference
		T* Clone(T* pPtr){
			++*mCounter;
			return pPtr;
		}

		/// Substract a reference, and delete pointer if needed
		void Release(T* pPtr){
			if(--*mCounter == 0){
				delete mCounter;
				delete pPtr;
			}
		}

		/// Swap two Refcount policies
		void Swap(RefCountPolicy &pRefCount){
			std::swap(pRefCount.mCounter, mCounter);
		}

	private:
		int* mCounter;
	};

	/// Resource-Type counting policy
	template<class T>
	class ResourceTypePolicy{
	public:
		/// Add a reference to resource
		T* Clone(T* pPtr){
			if(pPtr)
				pPtr->AddRef();
			return pPtr;
		}

		/// Release a reference to resource
		static void Release(T* pPtr){
			if(pPtr)
				pPtr->Release();
		}

		/// Swap. Do nothing
		void Swap(ResourceTypePolicy &p){
		}
	};
}
#endif
